package zhou.chs.p4_8;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    TextView tv1,tv2;  //显示温度、湿度数据
    private SerialPort serialPort;      //声明串口
    private ReadThread mReadThread;	//读取线程
    private Handler myhandler;       //信息通道
    byte[] rbuf = new byte[32];  //串口接收数据缓冲区
    int tn;   //3s计数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView)findViewById(R.id.idtv1);   //实例化控件
        tv2 = (TextView)findViewById(R.id.idtv2);
        serialPort = new SerialPort();   //创建串口
        //打开串口1，串口参数：9600,n,8,1
        serialPort.open("COM1",9600, 8, "N", 1);
        mReadThread = new ReadThread();   //声明串口接收数据线程
        mReadThread.start();    //启动串口接收数据线程
        myhandler = new MyHandler();     //实例化Handler，用于进程间的通信
        Timer mTimer = new Timer();        //新建Timer
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                tn++;                   //每3秒加1
                Message msg1 = myhandler.obtainMessage();   //创建消息
                msg1.what = 1;                      //变量what赋值
                myhandler.sendMessage(msg1);     //发送消息
            }
        }, 1000, 3000);     //延时1000ms，然后每隔3000ms发送消息
    }
    //读取数据的线程
    private class ReadThread extends Thread {
        @Override
        public void run() {
            super.run();
            byte[] buff = new byte[32];
            while(true){
                try {
                    int n = serialPort.read(buff,32,100);  //接收数据
                    if(n > 0) {
                        for (int i=0;i<n;i++){
                            rbuf[i] = buff[i];    //保存数据
                        }
                        Message msg0 = myhandler.obtainMessage();
                        msg0.what = 0;
                        myhandler.sendMessage(msg0);  //收到数据，发送消息
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //在主线程处理Handler传回来的message
    class MyHandler extends Handler {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:     //收到串口数据
                    if((rbuf[1]==3)&&(rbuf[2]==4)) {
                        if(rbuf[3]==0x80) tv1.setText("探头错误");
                        else {      //处理并显示数据
                            int m = (rbuf[3]<<8)|(rbuf[4]&0xFF);
                            tv1.setText(String.format("%.1f", (float) m / 10));
                            m =  (rbuf[5]<<8)|(rbuf[6]&0xFF);
                            tv2.setText(String.format("%.1f", (float) m / 10));
                        }
                    }
                    break;
                case 1:     //3s定时时间到
                    byte[] tbuf={(byte)0x01,(byte)0x03,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x02,(byte)0xC4,(byte)0x0B};
                    serialPort.write(tbuf,8); //发送数据
                    break;
            }
        }
    }
    @Override   //程序退出前关闭串口
    protected void onDestroy() {
        super.onDestroy();
        serialPort.close();
    }
}
